<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_AddFolderAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$parent = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));

        $moduleName = $this->getRequestParameter("folderType", $this->getRequestParameter("configModule"));

		$folder = m_backoffice_documents_Folder::createNew()->setLang($this->getRequestParameter("docLang"))->setModule($moduleName);
		$formValues = $this->getFormValues("form");

        $errors = array();

		if(count($formValues) > 0)
		{
			if($formValues["label"] == "")
			{
				$errors["label"] = $this->getLocale('backoffice.errors.required');
			}
			else
			{
				$folder->setLabel($formValues["label"])->save(false);
                $methodName = 'add'.ucfirst($moduleName).'Folder';
				$parent->$methodName($folder)->save(false);
				$errors["status"] = "success";
			}
		}
		
		$this->addToModel("errors", $errors);
		$this->addToModel("folder", $folder);		
		$this->addToModel("href", $this->getRequestParameter("href"));
		$this->setView('success');
	}
}